package com.sjht.android.bxgz;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class BxgzApp extends Application {
	
	private static BxgzApp mApp;
	private List<Activity> mActivitys = new ArrayList<Activity>();
	
	@Override
	public void onCreate() {
		super.onCreate();
		mApp = (BxgzApp) getApplicationContext();
		toastMgr.builder.init(mApp);
	}

	public static BxgzApp getSelf() {
		return mApp;
	}

	public void clearInfo() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}, 500);
	}

	public void exit() {
		clearInfo();
		for (Activity activity : mActivitys) {
			activity.finish();
		}
	}

	public void push(Activity c) {
		for (int i = 0; i < mActivitys.size(); i++) {
			if (mActivitys.get(i) == c) {
				return;
			}
		}
		mActivitys.add(c);
	}
	
//	public UpdateManager getUpdateManager() {
//		if (mUpdateManager == null) {
//			mUpdateManager = new UpdateManager(mFilterHandler);
//		}
//		return mUpdateManager;
//	}

	public void pull(Activity c) {
		for (int i = 0; i < mActivitys.size(); i++) {
			if (mActivitys.get(i) == c) {
				mActivitys.remove(i);
				return;
			}
		}
	}
	
//	private Handler mFilterHandler = new Handler() {
//
//		@Override
//		public void handleMessage(Message msg) {
//			Intent intent = null;
//			Upgrade upgrade = null;
//			switch (msg.what) {
//
//			case ZdjConstants.CODE_AUTO_UPDATE:
//				upgrade = (Upgrade) msg.obj;
//				if (upgrade.code != null && "100".equals(upgrade.code)
//						&& !StringUtils.isNullOrEmpty(upgrade.newversion)) {
//					intent = new Intent(getBaseContext(), UpdateActivity.class);
//					intent.putExtra(ZdjConstants.APK_FORCE_UPDATE, false);
//					intent.putExtra(ZdjConstants.APK_UPDATE_URL, upgrade.url);
//					intent.putExtra(ZdjConstants.APK_APP_VERSION, upgrade.newversion);
//					intent.putExtra(ZdjConstants.APK_APP_OLD_VERSION, Apn.version);
//					intent.putExtra(ZdjConstants.APK_APP_SIZE, upgrade.size);
//					intent.putExtra(ZdjConstants.APK_UPDATE_DESCRIBE, upgrade.describe);
//					intent.putExtra(ZdjConstants.APK_APP_NAME, upgrade.url.substring(upgrade.url.lastIndexOf('/') + 1));
//					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//					startActivity(intent);
//				}
//				break;
//
//			case ZdjConstants.CODE_MANUAL_UPDATE:
//				upgrade = (Upgrade) msg.obj;
//				if (upgrade.code != null && "100".equals(upgrade.code)) {
//					if (!StringUtils.isNullOrEmpty(upgrade.newversion) && Apn.version != null
//							&& !upgrade.newversion.equals(Apn.version)) {
//						intent = new Intent(getBaseContext(), UpdateActivity.class);
//						intent.putExtra(ZdjConstants.APK_FORCE_UPDATE, false);
//						intent.putExtra(ZdjConstants.APK_UPDATE_URL, upgrade.url);
//						intent.putExtra(ZdjConstants.APK_APP_VERSION, upgrade.newversion);
//						intent.putExtra(ZdjConstants.APK_APP_OLD_VERSION, Apn.version);
//						intent.putExtra(ZdjConstants.APK_APP_SIZE, upgrade.size);
//						intent.putExtra(ZdjConstants.APK_UPDATE_DESCRIBE, upgrade.describe);
//						intent.putExtra(ZdjConstants.APK_APP_NAME,
//								upgrade.url.substring(upgrade.url.lastIndexOf('/') + 1));
//						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//						startActivity(intent);
//					} else {
//						Utils.toast(getBaseContext(), "已经是最新版本啦！！！");
//					}
//				} else {
//					Utils.toast(getBaseContext(), "检测失败！！！");
//				}
//				break;
//
//			}
//		};
//	};
	
	public enum toastMgr {
		builder;

		private View v;
		private TextView tv;
		private Toast toast;

		private void init(Context c) {
			v = LayoutInflater.from(c).inflate(R.layout.toast, null);
			tv = (TextView) v.findViewById(R.id.tv_toast);
			toast = new Toast(c);
			toast.setView(v);
		}

		public void display(CharSequence text, int duration) {
			if (text.length() != 0) {
				tv.setText(text);
				toast.setDuration(duration);
				toast.setGravity(Gravity.BOTTOM, 0, 0);
				toast.show();
			}
		}

		public void display(int Resid, int duration) {
			if (Resid != 0) {
				tv.setText(Resid);
				toast.setDuration(duration);
				toast.show();
			}
		}
	}
}
